﻿using System;
using Microsoft.Phone.BackgroundAudio;

namespace Samples.Radio.Agent.Model
{
    public class PlaylistTrack
    {
        public Guid Id { get; set; }

        public string Source { set; get; }

        public string Title { set; get; }

        public string Artist { set; get; }

        public string Album { set; get; }

        public string AlbumArtUrl { set; get; }

        public bool IsStreaming { set; get; }

        public int CurrentPlaylistOrder { get; set; }

        public Uri AlbumArt
        {
            get
            {
                if (string.IsNullOrEmpty(AlbumArtUrl))
                {
                    return null;
                }
                if (AlbumArtUrl.StartsWith("http", StringComparison.InvariantCultureIgnoreCase))
                {
                    return new Uri(AlbumArtUrl);
                }
                return new Uri(AlbumArtUrl, UriKind.Relative);
            }
        }

        public EnabledPlayerControls PlayerControls { set; get; }

        public PlaylistTrack()
        {
            Id = Guid.NewGuid();
            CurrentPlaylistOrder = 0;
            Source = "";
            Title = "";
            Artist = "";
            Album = "";
            AlbumArtUrl = "";
            IsStreaming = true;
            PlayerControls = new EnabledPlayerControls();
        }

        // Convert from this class to the standard WP 7.1 AudioTrack
        public AudioTrack ToAudioTrack()
        {
            if (IsStreaming)
            {
                return new AudioTrack(null, Title, Artist, Album, AlbumArt, Source, PlayerControls);
            }
            return new AudioTrack(new Uri(Source), Title, Artist, Album, AlbumArt, null, PlayerControls);
        }
    }
}
